home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
FromTheMag
/
JW FLV MEDIA PLAYER 4.2
/
mediaplayer.exe
/
player.swf
/
scripts
/
com
/
jeroenwijering
/
models
/
RTMPModel.as
< prev
next >
Wrap
Text File
|
2008-11-04
|
11KB
|
322 lines
package com.jeroenwijering.models
{
import com.jeroenwijering.events.ModelEvent;
import com.jeroenwijering.events.ModelStates;
import com.jeroenwijering.player.Model;
import com.jeroenwijering.utils.NetClient;
import com.meychi.ascript.TEA;
import flash.events.AsyncErrorEvent;
import flash.events.ErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.SoundTransform;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.ObjectEncoding;
import flash.net.Responder;
import flash.utils.clearInterval;
import flash.utils.clearTimeout;
import flash.utils.setInterval;
public class RTMPModel implements ModelInterface
{
private var stream:NetStream;
private var timeinterval:Number;
private var timeout:Number;
private var connection:NetConnection;
private var model:Model;
private var transform:SoundTransform;
private var metadata:Boolean;
private var video:Video;
public function RTMPModel(param1:Model)
{
super();
model = param1;
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
connection.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler);
connection.objectEncoding = ObjectEncoding.AMF0;
connection.client = new NetClient(this);
video = new Video(320,240);
quality(model.config["quality"]);
transform = new SoundTransform();
if(model.config["mute"] == true)
{
volume(0);
}
else
{
volume(model.config["volume"]);
}
}
private function streamlengthHandler(param1:Number) : void
{
onData({
"type":"streamlength",
"duration":param1
});
}
private function metaHandler(param1:ErrorEvent) : void
{
model.sendEvent(ModelEvent.META,{"error":param1.text});
}
public function setStream() : void
{
var _loc1_:* = undefined;
var _loc2_:Responder = null;
var _loc3_:Responder = null;
var _loc4_:Responder = null;
_loc1_ = getID(model.playlist[model.config["item"]]["file"]);
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler);
stream.bufferTime = model.config["bufferlength"];
stream.client = new NetClient(this);
video.attachNetStream(stream);
stream.soundTransform = transform;
stream.play(_loc1_);
_loc2_ = new Responder(streamlengthHandler);
_loc3_ = new Responder(referrerHandler);
_loc4_ = new Responder(pageURLHandler);
connection.call("getStreamLength",_loc2_,_loc1_);
connection.call("getReferrer",_loc3_,_loc1_);
connection.call("getPageUrl",_loc4_,_loc1_);
connection.call("checkBandwidth",null);
clearInterval(timeinterval);
timeinterval = setInterval(timeHandler,100);
}
private function pageURLHandler(param1:String) : void
{
onData({
"type":"pageurl",
"url":param1
});
}
private function timeHandler() : void
{
var _loc1_:* = undefined;
var _loc2_:* = undefined;
var _loc3_:* = undefined;
_loc1_ = Math.round(stream.bufferLength / stream.bufferTime * 100);
_loc2_ = Math.round(stream.time * 10) / 10;
_loc3_ = model.playlist[model.config["item"]]["duration"];
if(_loc1_ < 100 && _loc2_ < Math.abs(_loc3_ - stream.bufferTime - 1))
{
model.sendEvent(ModelEvent.BUFFER,{"percentage":_loc1_});
if(model.config["state"] != ModelStates.BUFFERING)
{
connection.call("checkBandwidth",null);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
}
}
else if(model.config["state"] == ModelStates.BUFFERING)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
}
if(_loc3_ > 0)
{
model.sendEvent(ModelEvent.TIME,{
"position":_loc2_,
"duration":_loc3_
});
}
}
public function stop() : void
{
clearInterval(timeinterval);
connection.close();
if(stream)
{
stream.close();
}
video.attachNetStream(null);
}
private function statusHandler(param1:NetStatusEvent) : void
{
if(param1.info.code == "NetConnection.Connect.Success")
{
if(param1.info.secureToken != undefined)
{
connection.call("secureTokenResponse",null,TEA.decrypt(param1.info.secureToken,model.config["token"]));
}
if(model.config["subscribe"])
{
timeout = setInterval(subscribe,2000,model.playlist[model.config["item"]]["file"]);
}
else
{
setStream();
}
}
else if(param1.info.code == "NetStream.Seek.Notify")
{
clearInterval(timeinterval);
timeinterval = setInterval(timeHandler,100);
}
else if(param1.info.code == "NetStream.Play.StreamNotFound" || param1.info.code == "NetConnection.Connect.Rejected" || param1.info.code == "NetConnection.Connect.Failed")
{
stop();
model.sendEvent(ModelEvent.ERROR,{"message":"Stream not found: " + model.playlist[model.config["item"]]["file"]});
}
else
{
model.sendEvent(ModelEvent.META,{"info":param1.info.code});
}
}
private function errorHandler(param1:ErrorEvent) : void
{
model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
}
public function load() : void
{
model.mediaHandler(video);
connection.connect(model.config["streamer"]);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
}
private function referrerHandler(param1:String) : void
{
onData({
"type":"referrer",
"url":param1
});
}
public function onData(param1:Object) : void
{
if(param1.type == "metadata" && !metadata)
{
if(param1.width)
{
video.width = param1.width;
video.height = param1.height;
}
if(model.playlist[model.config["item"]]["start"] > 0)
{
seek(model.playlist[model.config["item"]]["start"]);
}
metadata = true;
}
else if(param1.type == "complete")
{
clearInterval(timeinterval);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
}
else if(param1.type == "fcsubscribe")
{
if(param1.code == "NetStream.Play.StreamNotFound")
{
stop();
model.sendEvent(ModelEvent.ERROR,{"message":"Subscription failed: " + model.playlist[model.config["item"]]["file"]});
}
else if(param1.code == "NetStream.Play.Start")
{
setStream();
}
clearInterval(timeout);
}
model.sendEvent(ModelEvent.META,param1);
}
private function getID(param1:String) : String
{
if(param1.substr(-4) == ".mp3")
{
param1 = "mp3:" + param1.substr(0,param1.length - 4);
}
else if(param1.substr(-4) == ".mp4" || param1.substr(-4) == ".mov" || param1.substr(-4) == ".aac" || param1.substr(-4) == ".m4a")
{
param1 = "mp4:" + param1.substr(0,param1.length - 4);
}
else if(param1.substr(-4) == ".flv")
{
param1 = param1.substr(0,param1.length - 4);
}
return param1;
}
public function volume(param1:Number) : void
{
transform.volume = param1 / 100;
if(stream)
{
stream.soundTransform = transform;
}
}
public function play() : void
{
clearTimeout(timeout);
clearInterval(timeinterval);
stream.resume();
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
timeinterval = setInterval(timeHandler,100);
}
private function subscribe(param1:String) : void
{
connection.call("FCSubscribe",null,param1);
}
public function seek(param1:Number) : void
{
clearTimeout(timeout);
clearInterval(timeinterval);
if(model.config["state"] == ModelStates.PAUSED)
{
stream.resume();
}
else
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
}
stream.seek(param1);
}
public function pause() : void
{
clearInterval(timeinterval);
stream.pause();
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
}
public function quality(param1:Boolean) : void
{
if(param1 == true)
{
video.smoothing = true;
video.deblocking = 3;
}
else
{
video.smoothing = false;
video.deblocking = 1;
}
}
}
}